package bear.download;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;

public class HttpsUtils {
    private X509TrustManager xtm = new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] chain, String authType) {}

            public void checkServerTrusted(X509Certificate[] chain, String authType) {;
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
    }; 
    private HostnameVerifier hnv = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }; 
    public HttpsUtils() {
        SSLContext sslContext = null;

        try {
            sslContext = SSLContext.getInstance("TLS");
            X509TrustManager[] xtmArray = new X509TrustManager[] { xtm };
            sslContext.init(null, xtmArray, new java.security.SecureRandom());
        } catch(GeneralSecurityException gse) {
        }
        if(sslContext != null) {
            HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
        }

        HttpsURLConnection.setDefaultHostnameVerifier(hnv);
    }
	public String get(String urlPath) {
	StringBuffer json=new StringBuffer();
	URL url = null;
	HttpsURLConnection urlCon = null;
	BufferedReader in = null;
	try {
    url = new URL(urlPath);
    urlCon = (HttpsURLConnection) url.openConnection();
         urlCon.setReadTimeout(30000);
            urlCon.setConnectTimeout(30000);
            urlCon.setDoOutput(true);
            urlCon.setDoInput(true);
            urlCon.setRequestMethod("GET");
            urlCon.setRequestProperty("accept", "*/*");
            in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
            String line;
            while((line = in.readLine()) != null) {
                json.append(line);
            }
        } catch(MalformedURLException mue) {
            mue.printStackTrace();
        } catch(IOException ioe) {
            ioe.printStackTrace();
        } catch(Exception e) {
            e.printStackTrace();
        }finally{
         if(null != in){
          try {
     in.close();
     in = null;
    } catch (IOException e) {
     e.printStackTrace();
    }
         }
         if(null != urlCon){
          urlCon.disconnect();
          urlCon = null;
         }
        }
        return json.toString();
    }
	
	public String post(String urlPath,String params) {
		StringBuffer json=new StringBuffer();
		URL url = null;
		HttpsURLConnection urlCon = null;
		BufferedReader in = null;
		byte[] postData = params.getBytes();
		try {
	    url = new URL(urlPath);
	    urlCon = (HttpsURLConnection) url.openConnection();
	         urlCon.setReadTimeout(30000);
	            urlCon.setConnectTimeout(30000);
	            urlCon.setDoOutput(true);
	            urlCon.setDoInput(true);
	            urlCon.setUseCaches(false);
	            urlCon.setRequestMethod("POST");
	            urlCon.setRequestProperty("accept", "*/*");
	            urlCon.setInstanceFollowRedirects(true);
	            urlCon.connect();
	            DataOutputStream dos = new DataOutputStream(urlCon.getOutputStream());
	            dos.write(postData);
	            System.out.println(params);
	            dos.flush();
	            dos.close();
	            if (urlCon.getResponseCode() == 200){
	            	BufferedReader receiveData = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
	            	String line;
	            	StringBuffer result=new StringBuffer();
	            	while((line = receiveData.readLine()) != null) {
	                    result.append(line);
	                }
	            	System.out.println(result);
	            	
	            	
	            }
	            /*in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
	            String line;
	            while((line = in.readLine()) != null) {
	                json.append(line);
	            }*/
	        } catch(MalformedURLException mue) {
	            mue.printStackTrace();
	        } catch(IOException ioe) {
	            ioe.printStackTrace();
	        } catch(Exception e) {
	            e.printStackTrace();
	        }finally{
	         if(null != in){
	          try {
	     in.close();
	     in = null;
	    } catch (IOException e) {
	     e.printStackTrace();
	    }
	         }
	         if(null != urlCon){
	          urlCon.disconnect();
	          urlCon = null;
	         }
	        }
	        return json.toString();
	    }
}

